import time
import uuid
import hashlib
import requests
import threading
url = 'http://hetaojia.corp.hetao101.com/status?id={0}&crmid={1}'
status=True
def get_mac_address():
    mac = uuid.getnode()
    mac_address = ':'.join(['{:02x}'.format((mac >> i) & 0xff) for i in range(0, 48, 8)])
    return mac_address

def hash_mac_address(mac_address):
    sha256_hash = hashlib.sha256(mac_address.encode()).hexdigest()
    return sha256_hash

def save_machine_code(code,crmid):
    print("线程启动")
    # with open('machine_code.txt', 'w') as file:
    #     file.write(machine_code)
    while status:
        response = requests.get(url.format(code,crmid))  # 使用get方法发起请求
        #print(response.content.decode('utf-8'))  # 输出请求结果
        time.sleep(1)
    # response = requests.post(url) #使用post发起请求
        #print(response.status_code)  # 输出请求状态码

def close_heart():
    status=False

def start_heart(crmid):
    #print("启动线程")
    code=hash_mac_address(get_mac_address())
    sing_process = threading.Thread(target=save_machine_code,kwargs={"code":code,"crmid":crmid})
    #print(sing_process)
    sing_process.start()
    #print(sing_process)


def getcrmid():
    te = requests.get("http://hetaojia.corp.hetao101.com/getcrmid?id="+hash_mac_address(get_mac_address())).json()
    if len(te['data'])>0:
        return str(te['data'][0]['crmid'])
    else:
        return 0


#print(getcrmid())
# mac_address = get_mac_address()
# machine_code = hash_mac_address(mac_address)
# save_machine_code(machine_code)
#save_machine_code(hash_mac_address(get_mac_address()))


#start_heart(30007523)
